package com.million.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.million.eduservice.entity.EduCourse;
import com.million.eduservice.entity.EduCourseDescription;
import com.million.eduservice.entity.frontvo.course.CourseQueryVo;
import com.million.eduservice.entity.frontvo.course.CourseWebVo;
import com.million.eduservice.entity.vo.course.CourseInfoForm;
import com.million.eduservice.entity.vo.course.CourseInfoList;
import com.million.eduservice.entity.vo.course.CoursePublishVo;
import com.million.eduservice.entity.vo.course.CourseQuery;
import com.million.eduservice.mapper.EduCourseMapper;
import com.million.eduservice.service.EduChapterService;
import com.million.eduservice.service.EduCourseDescriptionService;
import com.million.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.million.eduservice.service.EduVideoService;
import com.million.servicebase.exception.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 十麓
 * @since 2022-03-24
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduCourseMapper eduCourseMapper;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    @Override
    public String addCourseInfo (CourseInfoForm courseInfoForm) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm, eduCourse);
        //添加到课程的信息
        int insert = baseMapper.insert(eduCourse);
        if (insert == 0) {
            throw new GuliException(20001, "添加课堂失败");
        }
        //添加成功   获取课程的id
        String courseId = eduCourse.getId();

        //添加描述信息到课程介绍表中
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        //不为空
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        eduCourseDescription.setId(courseId);
        eduCourseDescriptionService.save(eduCourseDescription);

        return courseId;
    }


    @Override
    public CourseInfoForm getCourseInfo (String id) {

        //1.查询课程信息
        EduCourse eduCourse = baseMapper.selectById(id);
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        if (eduCourse != null) {
            BeanUtils.copyProperties(eduCourse, courseInfoForm);
        } else {
            throw new GuliException(20001, "查询课程信息失败");
        }

        //2.查询课程描述信息
        EduCourseDescription courseDescription = eduCourseDescriptionService.getById(id);
        courseInfoForm.setDescription(courseDescription.getDescription());

        return courseInfoForm;
    }

    //修改课程信息
    @Override
    public void updateCourseInfo (CourseInfoForm courseInfoForm) {

        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm, eduCourse);
        int i = baseMapper.updateById(eduCourse);
        if (i == 0) {
            throw new GuliException(20001, "修改失败");
        }
        //修改课程简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoForm.getId());
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }

    //根据课程id查询课程确认信息
    @Override
    public CoursePublishVo getPublishCourseInfo (String courseId) {
        CoursePublishVo publishCourseInfo = eduCourseMapper.getPublishCourseInfo(courseId);
        if (publishCourseInfo == null) {
            throw new GuliException(20001, "确认信息失败");
        }
        return publishCourseInfo;
    }

    //课程提交，根据id将课程表中的status设置为已发布状态
    @Override
    public void publicCourse (String courseId) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        baseMapper.updateById(eduCourse);
    }

    @Override
    public Page<CourseInfoList> getCourseInfoList (Page<CourseInfoList> courseListPage, QueryWrapper<CourseInfoList> queryWrapper) {
        return null;
    }


    //条件分页查询课程信息
    @Override
    public Page<CourseInfoList> pageQueryCourse (Page<CourseInfoList> courseListPage, CourseQuery courseQuery) {
        QueryWrapper<CourseInfoList> queryWrapper = new QueryWrapper<>();
        //判断teacherQuery是否为空，也就是前端有无传过来参数,没有，就直接分页查询
        if (courseQuery == null) {
            Page<CourseInfoList> courseInfoList = baseMapper.getCourseInfoList(courseListPage, null);
            return courseInfoList;
        }
        String title = courseQuery.getName();
        String status = courseQuery.getStatus();
        Integer lessonNum = courseQuery.getLessonNum();
        BigDecimal price = courseQuery.getPrice();

        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(status)) {
            queryWrapper.eq("status", status);
        }
        if (!StringUtils.isEmpty(lessonNum)) {
            //课程数大于等于  这里的column是数据库的字段名
            queryWrapper.ge("lesson_num", lessonNum);
        }
        if (!StringUtils.isEmpty(price)) {
            //价格大于等于
            queryWrapper.ge("price", price);
        }
        return baseMapper.getCourseInfoList(courseListPage, queryWrapper);
    }

    //删除课程
    @Override
    @Transactional  //添加事务  确保这些代码执行完才从数据库中删掉数据
    public void deleteCourseInfo (String courseId) {
        //1.根据课程id删除小节
        eduVideoService.removeVideoById(courseId);
        //2.根据课程id删除章节;
        eduChapterService.removeChapterById(courseId);
        //3.根据课程id删除课程描述
        eduCourseDescriptionService.removeById(courseId);
        //4.根据课程id删除课程本身
        baseMapper.deleteById(courseId);
    }

    /******************************前台****************************************/
    //根据id降序，查询前8条热门课程
    //写入到缓存中
    @Cacheable(value = "course", key = "'selectHotCourse'")
    @Override
    public List<EduCourse> selectHotCourse () {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id").last("limit 8");
        List<EduCourse> courseList = baseMapper.selectList(wrapper);
        return courseList;
    }

    //分页查询课程信息
    @Override
    public Map<String, Object> getCourseList (Page<EduCourse> coursePage, CourseQueryVo courseQueryVo) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        //判断一级分类是否为空
        if (!StringUtils.isEmpty(courseQueryVo.getSubjectParentId())) {
            queryWrapper.eq("subject_parent_id", courseQueryVo.getSubjectParentId());
        }
        //判断二级分类是否为空
        if (!StringUtils.isEmpty(courseQueryVo.getSubjectId())) {
            queryWrapper.eq("subject_id", courseQueryVo.getSubjectId());
        }
        //判断关注度是否为空
        if (!StringUtils.isEmpty(courseQueryVo.getBuyCountSort())) {
            queryWrapper.orderByDesc("buy_count");
        }
        //判断最新是否为空
        if (!StringUtils.isEmpty(courseQueryVo.getGmtCreateSort())) {
            queryWrapper.orderByDesc("gmt_create");
        }
        //判断价格是否为空
        if (!StringUtils.isEmpty(courseQueryVo.getPriceSort())) {
            queryWrapper.orderByDesc("price");
        }
        baseMapper.selectPage(coursePage, queryWrapper);
        List<EduCourse> records = coursePage.getRecords();

        long current = coursePage.getCurrent();
        long pages = coursePage.getPages();
        long size = coursePage.getSize();
        long total = coursePage.getTotal();
        boolean hasNext = coursePage.hasNext();
        boolean hasPrevious = coursePage.hasPrevious();
        Map<String, Object> map = new HashMap<>();
        map.put("records", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    //根据d 获取课程信息，编写sql语句
    @Override
    public CourseWebVo getBaseCourseInfo (String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }

    //根据课程名查询课程id，通过课程id 找到课程详情
    @Override
    public String queryCourseByName (String courseName) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("title",courseName);
        EduCourse eduCourse = baseMapper.selectOne(wrapper);
        return eduCourse.getId();
    }
}


